//
//  EmoticonManager.swift
//  emoticonKB
//
//  Created by mac on 16/3/21.
//  Copyright © 2016年 mac. All rights reserved.
//

//---------------负责加载表情数据---------------

import UIKit

let EmoticonCount = 21

let EmoticonColoumCount = 7
let EmoticonRowCount = 3

class EmoticonManager: NSObject {
    
    //单例
    static let sharedEmoticonManager = EmoticonManager()
    
 
    var packages = [EmoticonPackages]()
    
    //私有化构造函数
    override init() {
    
        super.init()
        self.loadEmoticons()
    }
    
    
    func loadEmoticons() {
    
        //获取表情文件的路径
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        //读取文件中的字典数据
        let dict = NSDictionary(contentsOfFile: path)!
        //        print(dict)
        
        //读取字典中对应的packages数组
        let packages = dict["packages"] as! [[String : AnyObject]]
//        print(packages)
        //遍历数组获取id
        for p in packages {
        
            let id = p["id"] as! String
            //加载分组文件目录中 info.plist文件
            loadGroupEmoticons(id)
        }
    }
    
    private func loadGroupEmoticons(id: String) {
    
        let path = NSBundle.mainBundle().pathForResource("Info.plist", ofType: nil, inDirectory: "Emoticons.bundle/" + id)!
//        print(path)
        //将indo.plist文件转换为字典
        let dict = NSDictionary(contentsOfFile: path)!
//        print(dict)
        
        let name = dict["group_name_cn"] as! String
        
        let array = dict["emoticons"] as! [[String : String]]
        
        let p = EmoticonPackages(id: id,name: name, array: array)
        
        //添加到数组中
        packages.append(p)
    }

}
